package cn.zhibang.yygh2.service.hosp.controller;

import cn.zhibang.yygh2.common.util.util.MD5;
import cn.zhibang.yygh2.common.util.result.Result;
import cn.zhibang.yygh2.model.model.hosp.HospitalSet;
import cn.zhibang.yygh2.model.vo.hosp.HospitalQueryVo;
import cn.zhibang.yygh2.model.vo.hosp.HospitalSetQueryVo;
import cn.zhibang.yygh2.service.hosp.service.HospitalService;
import cn.zhibang.yygh2.service.hosp.service.HospitalSetService;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/*
医院设置接口
 */
@Api(tags = "医院设置管理")
//返回的json格式
@RestController
//请求路径
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Resource
    private HospitalSetService hospitalSetService;



    /**
     * 1.查询医院设置表所有信息
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation(value = "获得所有医院设置")
    public Result findAllHospitalSet(){
        //调用service的方法
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }


    /**
     * 2,逻辑删除医院设置
     * @param id
     * @return
     */
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("/{id}")
    public Result removeHospSet(@PathVariable long id){

        boolean b = hospitalSetService.removeById(id);

        if (b){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 3.条件查询带分页
     * @param current
     * @param limit
     * @param hospitalSetQueryVo
     * @return
     */
    @ApiOperation(value = "条件查询带分页")
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current, limit);
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hospname = hospitalSetQueryVo.getHosname();
        String hospcode = hospitalSetQueryVo.getHoscode();

//        if (StringUtils.isEmpty(hospname)){
//            wrapper.like("hosname",hospitalSetQueryVo.getHosname());
//        }
//        if (StringUtils.isEmpty(hospcode)){
//            wrapper.like("hoscode",hospitalSetQueryVo.getHoscode());
//        }
        wrapper.orderByDesc("create_time");     //按照创建时间降序
        //调用方法实现分页查询
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, wrapper);


        //返回结果
        return Result.ok(hospitalSetPage);
    }

    /**
     * 4.添加医院设置
     * @param hospitalSet
     * @return
     */
    @ApiOperation(value = "添加医院设置")
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态 0使用 1不能使用
        hospitalSet.setStatus(0);
        //签名密钥
        Random random = new Random();
        //生成密钥规则:当前时间毫秒数 + 100以内的随机数
        String signkey = MD5.encrypt(System.currentTimeMillis()  + "" + random.nextInt(1000));
        hospitalSet.setSignKey(signkey);

        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            //TODO: 调用短信发送业务方法
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


    /**
     * 5.根据id获取医院设置
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取医院设置")
    @GetMapping("/getHospSet/{id}")
    public Result getHospSet(@PathVariable long id){
        HospitalSet byId = hospitalSetService.getById(id);
        return Result.ok(byId);
    }

    /**
     * 6,修改医院设置
     * @param hospitalSet
     * @return
     */
    @ApiOperation(value = "修改医院设置")
    @PostMapping("/uodateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){

        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }

    /**
     * 7.批量删除医院设置
     * @param idList
     * @return
     */
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("/bathRemove")
    public Result bathRemoveHospitalSet(@RequestBody List<Long> idList){
        boolean b = hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    /**
     * 8.医院设置锁定和解锁
     * @param id
     * @param status
     * @return
     */
    @ApiOperation(value = "医院设置锁定和解锁")
    @PostMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable long id,
                                  @PathVariable Integer status){
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        boolean b = hospitalSetService.updateById(hospitalSet);
        return  Result.ok();
    }



    /**
     * 9 发送签名密钥
     * @param id
     * @return
     */
    @ApiOperation(value = "发送签名密钥")
    @PutMapping("sendKey/{id}")
    public Result sendHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey(); //得到签名密钥
        String hoscode = hospitalSet.getHoscode(); //得到医院编号
        //TODO 发送短信

        return Result.ok();
    }









}
